/**
 * kl yy-ds @2021
 */
package com.cxps.server.vip.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cxps.cloud.enums.DataStatus;
import com.cxps.cloud.utils.IdUtils;
import com.cxps.server.user.domain.UserEntity;
import com.cxps.server.vip.domain.VIPInfo;
import com.cxps.server.vip.domain.VIPInfoQuery;
import com.cxps.server.vip.mapper.VIPMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 会员逻辑层
 *
 * @author YouCai.Liu
 * @since 2021/11/7
 * ...
 */
@Service
public class VIPService {

    @Autowired
    private VIPMapper vipMapper;

    /**
     * 会员列表
     *
     * @param query
     * @return
     */
    public Page list(VIPInfoQuery query) {
        // 分页查询对象
        Page<VIPInfo> page = new Page<>(query.getCurrent(), query.getSize());

        // 条件构造器
        QueryWrapper<VIPInfo> qw = new QueryWrapper<>();
        qw.orderByDesc("update_time");
        if (StringUtils.isNotBlank(query.getVipPhone())) {
            qw.eq("vip_phone", query.getVipPhone());
        }
        if (StringUtils.isNotBlank(query.getVipLevel())) {
            qw.eq("vip_level", query.getVipLevel());
        }
        return vipMapper.selectPage(page, qw);
    }

    /**
     * 新增会员
     *
     * @param info
     */
    public void add(VIPInfo info) {
        // 验证唯一
        singleVIP(info.getVipPhone());

        // 执行添加
        info.setId(IdUtils.generateId());
        info.preInsert();
        vipMapper.insert(info);
    }

    /**
     * 会员信息修改
     *
     * @param info
     */
    public void update(VIPInfo info) {
        // 验证唯一
        singleVIP(info.getVipPhone());

        // 执行修改
        info.preUpdate();
        vipMapper.updateById(info);
    }

    /**
     * 验证会员唯一
     */
    public void singleVIP(String phone) {
        QueryWrapper<VIPInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        queryWrapper.eq("vip_phone", phone);
        VIPInfo vipInfo = vipMapper.selectOne(queryWrapper);
        if (vipInfo != null) {
            throw new IllegalArgumentException("手机号已存在");
        }
    }

    /**
     * 根据会员ID查询会员信息
     *
     * @param id
     * @return
     */
    public VIPInfo getVipById(String id) {
        return vipMapper.selectById(id);
    }

    /**
     * 冻结会员
     *
     * @param id
     */
    public void freeze(String id) {
        vipMapper.updateById(freezeOrUnfreeze(id, DataStatus.OFF_LINE));
    }

    /**
     * 解冻会员
     *
     * @param id
     */
    public void unfreeze(String id) {
        vipMapper.updateById(freezeOrUnfreeze(id, DataStatus.ON_LINE));
    }

    /**
     * 构建解冻、冻结会员对象
     *
     * @param id
     * @param status
     * @return
     */
    private VIPInfo freezeOrUnfreeze(String id, DataStatus status) {
        VIPInfo info = new VIPInfo();
        info.setId(id);
        info.preUpdate();
        info.setStatus(status.getCode());
        return info;
    }

    /**
     * 查询用户
     *
     * @param username
     * @return
     */
    public VIPInfo loadUserByUsername(String username) {
        QueryWrapper<VIPInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "vip_name", "vip_phone", "password");
        queryWrapper.eq("vip_phone", username);
        return vipMapper.selectOne(queryWrapper);
    }
}
